﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using Microsoft.Xna.Framework.Graphics;
using TomShane.Neoforce.Controls;

namespace tactics.GUI
{
    using PlayerList = List<Player>;
    using PlayerInfoList = List<PlayerInfo>;
    using DisplayList = List<MiniDisplay>;

    struct PlayerInfo
    {
        public Player      player;
        public DisplayList displayList;

        public PlayerInfo(Player playerIn)
        {
            player = playerIn;
            displayList = new DisplayList();
        }
    }

    public class MiniDisplayManager
    {
        Manager     guiManager;
        string      xmlFile;
        string      statusXmlFile;

        PlayerInfoList playerList;

        public MiniDisplayManager(Manager guiManagerIn, string xmlFileIn, string statusXmlFileIn)
        {
            guiManager = guiManagerIn;
            xmlFile = xmlFileIn;
            statusXmlFile = statusXmlFileIn;

            playerList = new PlayerInfoList();
        }

        public void Remove()
        {
            foreach (var p in playerList)
                foreach (var d in p.displayList)
                    d.Remove();
        }

        public void AddPlayer(Player player)
        {
            PlayerInfo pi = new PlayerInfo(player);
            InitializeControls(pi);
            
            playerList.Add(pi);
            LayoutControls();
        }

        private void InitializeControls(PlayerInfo playerInfo)
        {
            for (int i = 0; i < playerInfo.player.Units.Count; ++i)
            {
                Unit unit = playerInfo.player.GetUnit(i);

                playerInfo.displayList.Add(new MiniDisplay(guiManager, xmlFile, statusXmlFile, unit, playerInfo.player, playerInfo.player.Colour));
            }
        }

        public void LayoutControls()
        {
            int index = 0;
            int width = 0;
            int height = 0;
            
            int left = 0;
            int right = 0;

            foreach (PlayerInfo playerInfo in playerList)
            {
                if (playerInfo.player.Units.Count > 0)
                {
                    width = playerInfo.displayList[0].Width;
                    height = TacticsEngine.Instance.GraphicsManager.PreferredBackBufferHeight - playerInfo.displayList[0].Height - 2;
                    right = TacticsEngine.Instance.GraphicsManager.PreferredBackBufferWidth;

                    if (index == 0)
                        left = 2;
                    else
                        left = right - ((2 + width) * playerInfo.player.Units.Count);

                    for (int i = 0; i < playerInfo.displayList.Count; ++i)
                    {
                        playerInfo.displayList[i].UpdatePosition(left + ((2 + width) * i), height);
                    }
                }

                ++index;
            }
        }

        public void Update(PlayerList players)
        {
            // updating the minidisplays is done by reconstructing them, as they 
            // are only updated once per turn and its cleaner

            foreach (var playerInfo in playerList)
            {
                foreach (var display in playerInfo.displayList)
                {
                    display.Remove();
                }
                playerInfo.displayList.Clear();
            }

            playerList = new PlayerInfoList();

            for (int i = 0; i < players.Count; ++i)
                AddPlayer(players[i]);

            LayoutControls();            
        }
    }
}
